$import( "SmitearWebGL.Objects.Canvas.ScreenQuad" );
$import( "SmitearWebGL.Programs.Hdr.LuminanceProgram" );

SmitearWebGL.Objects.Canvas.Hdr.Luminance = SmitearWebGL.Objects.Canvas.ScreenQuad.extend(
{
	/******** constructor ********/

	init : function()
	{
		this._super();
		this._registClass( "SmitearWebGL.Objects.Canvas.Hdr.Luminance" );
		
		this._greyDownTech = null;
		
		this._offsets = null;
	}
	,
	calculateOffsets : function()
	{
		var hdrTexture =  this.getTextureAt( 0 );
		
		if ( ! hdrTexture ) return;
		
		var sU = 1.0 / hdrTexture.getWidth();
		var sV = 1.0 / hdrTexture.getHeight();
		
		this._offsets = new Float32Array( 16 );
		
		this._offsets[0] = -0.5 * sU;
		this._offsets[1] = 0.5 * sV;
		this._offsets[2] = 0.0;
		this._offsets[3] = 0.0;
		
		this._offsets[4] = 0.5 * sU;
		this._offsets[5] = 0.5 * sV;
		this._offsets[6] = 0.0;
		this._offsets[7] = 0.0;
		
		this._offsets[8] = -0.5 * sU;
		this._offsets[9] = -0.5 * sV;
		this._offsets[10] = 0.0;
		this._offsets[11] = 0.0;
		
		this._offsets[12] = 0.5 * sU;
		this._offsets[13] = -0.5 * sV;
		this._offsets[14] = 0.0;
		this._offsets[15] = 0.0;
	}
	,
	
	//override render function
	renderCallBack : function()
	{
		var program = this._greyDownTech;
		
		var hr = SmitearWebGL.Core.HResult.SUCCESS;

		hr |= program.activeShader();
		hr |= program.activeUniform( this );
		
		hr |= this._vertexBuffer.bind();
		hr |= this._vertexBuffer.active( program.getShader() );
		
		this.context.gl.drawArrays( this.context.gl.TRIANGLE_STRIP, 0, 4 );
		
		return hr;
	}
	,
	_createResource : function( params )
	{
		this._super();
		
		this._greyDownTech = this.context.programManager.getResourceByName( SmitearWebGL.Programs.InternalPrograms.Type.HDR_LUMINANCE );
	}
	,
	_destroyResource : function()
	{
		this._super();
		
		this._greyDownTech = null;
		
		this._offsets = null;
	}
}
);
